var a = require("../../utils/config.js");

Page({
    data: {
        taskDetail: {},
        userTaskId: "",
        isTaskReport: !1
    },
    onLoad: function(a) {
        var t = this;
        t.setData({
            userTaskId: a.id
        }), t.loadDataSource();
    },
    onReady: function() {},
    onShow: function() {
        this.loadDataSource();
    },
    onHide: function() {},
    onUnload: function() {},
    onPullDownRefresh: function() {},
    onReachBottom: function() {},
    onShareAppMessage: function() {},
    loadDataSource: function() {
        var t = this, e = wx.getStorageSync("userId");
        wx.showLoading({
            mask: !0
        });
        var s = {
            userId: e,
            id: t.data.userTaskId
        };
        wx.request({
            url: a.urlList.API_TASK_GET_MY_DETAIL,
            method: "GET",
            header: {
                contentType: "application/json"
            },
            data: s,
            success: function(a) {
                if (wx.hideLoading(), 200 == a.data.status) {
                    var e = !1;
                    a.data.data.task.userTaskStatus >= 1 && (a.data.data.task.acceptanceRequirements.indexOf("照片") >= 0 || a.data.data.task.acceptanceRequirements.indexOf("签到") >= 0 || a.data.data.task.acceptanceRequirements.indexOf("文字报告") >= 0) && (e = !0), 
                    t.setData({
                        taskDetail: a.data.data,
                        isTaskReport: e
                    });
                } else wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        });
    },
    cancelSign: function() {
        var t = this, e = wx.getStorageSync("userId");
        wx.showLoading({
            mask: !0
        });
        var s = {
            userId: e,
            id: t.data.userTaskId
        };
        wx.request({
            url: a.urlList.API_TASK_CANCEL_SIGN,
            method: "POST",
            data: s,
            header: {
                contentType: "application/json"
            },
            success: function(a) {
                wx.hideLoading(), 200 == a.data.status ? (wx.showToast({
                    title: "取消报名成功"
                }), t.onPullDownRefresh()) : wx.showToast({
                    title: a.data.message,
                    icon: "none"
                });
            }
        });
    },
    goToTaskReport: function() {
        wx.navigateTo({
            url: "taskReport?id=" + this.data.userTaskId
        });
    },
    finished: function() {
        var t = this, e = wx.getStorageSync("userId");
        if (t.data.isTaskReport) {
            var s = new Object();
            if (s.files = "", s.userId = wx.getStorageSync("userId"), s.userTaskId = t.data.userTaskId, 
            t.data.taskDetail.task.acceptanceRequirements.indexOf("照片") >= 0) {
                if (!t.data.taskDetail.task.taskReport.imageList || 0 == t.data.taskDetail.task.taskReport.imageList.length) return void wx.showToast({
                    title: "请完成任务汇报",
                    icon: "none"
                });
                t.data.taskDetail.task.taskReport.imageList && t.data.taskDetail.task.taskReport.imageList.forEach(function(a, t) {
                    s.files += a.id + ",";
                });
            }
            if (t.data.taskDetail.task.acceptanceRequirements.indexOf("签到") >= 0) {
                if (!t.data.taskDetail.task.taskReport.mobileSign.address) return void wx.showToast({
                    title: "请完成任务汇报",
                    icon: "none"
                });
                s.lng = t.data.taskDetail.task.taskReport.mobileSign.lng, s.lat = t.data.taskDetail.task.taskReport.mobileSign.lat, 
                s.address = t.data.taskDetail.task.taskReport.mobileSign.address;
            }
            if (t.data.taskDetail.task.acceptanceRequirements.indexOf("文字报告") >= 0) {
                if (!t.data.taskDetail.task.taskReport.mobileSign.reson) return void wx.showToast({
                    title: "请完成任务汇报",
                    icon: "none"
                });
                s.reson = t.data.taskDetail.task.taskReport.mobileSign.reson;
            }
            console.log(s), wx.showLoading({
                mask: !0
            }), wx.request({
                url: a.urlList.API_MOBILESIGN_SIGN,
                method: "POST",
                data: s,
                header: {
                    contentType: "application/json"
                },
                success: function(s) {
                    if (200 == s.data.status) {
                        var n = {
                            userId: e,
                            id: t.data.userTaskId
                        };
                        wx.request({
                            url: a.urlList.API_TASK_FINISH_TASK,
                            method: "POST",
                            data: n,
                            header: {
                                contentType: "application/json"
                            },
                            success: function(a) {
                                wx.hideLoading(), 200 == a.data.status ? (wx.showToast({
                                    title: "完成任务提交成功"
                                }), wx.navigateBack()) : wx.showToast({
                                    title: a.data.message,
                                    icon: "none"
                                });
                            }
                        });
                    } else 415 == s.data.status ? (wx.hideLoading(), wx.showModal({
                        title: "系统提示",
                        content: s.data.message,
                        cancelText: "下次再说",
                        confirmText: "立即完善",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/editBank"
                            });
                        }
                    })) : 416 == s.data.status ? (wx.hideLoading(), wx.showModal({
                        title: "系统提示",
                        content: s.data.message,
                        cancelText: "下次再说",
                        confirmText: "实名认证",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/realNameAuthentication"
                            });
                        }
                    })) : wx.showToast({
                        title: s.data.message,
                        icon: "none"
                    });
                }
            });
        } else {
            wx.showLoading({
                mask: !0
            });
            var n = {
                userId: e,
                id: t.data.userTaskId
            };
            wx.request({
                url: a.urlList.API_TASK_FINISH_TASK,
                method: "POST",
                data: n,
                header: {
                    contentType: "application/json"
                },
                success: function(a) {
                    wx.hideLoading(), 200 == a.data.status ? (wx.showToast({
                        title: "完成任务提交成功"
                    }), wx.navigateBack()) : 415 == a.data.status ? wx.showModal({
                        title: "系统提示",
                        content: a.data.message,
                        cancelText: "下次再说",
                        confirmText: "立即完善",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/editBank"
                            });
                        }
                    }) : 416 == a.data.status ? wx.showModal({
                        title: "系统提示",
                        content: a.data.message,
                        cancelText: "下次再说",
                        confirmText: "实名认证",
                        success: function(a) {
                            a.confirm && wx.navigateTo({
                                url: "/pages/my/realNameAuthentication"
                            });
                        }
                    }) : wx.showToast({
                        title: a.data.message,
                        icon: "none"
                    });
                }
            });
        }
    },
    goToRecordNumber: function() {
        wx.navigateTo({
            url: "recordNumber?userTaskId=" + this.data.userTaskId
        });
    }
});